package com.c201801020208.ybl.User.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.AlertDialog.Builder;
import android.widget.TextView;
import android.widget.Toast;

import com.c201801020208.ybl.R;
import com.c201801020208.ybl.Tables.UserInfo;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class addWanted extends AppCompatActivity {
    private TextView textView;
    private TextView textView2;
    private TextView textView3;
    private TextView school;
    private TextView projectexperience;

    private TextView address;
    private TextView wantedjob;
    private TextView workexperience;
    private TextView discribe;
    private TextView major;
    private TextView ty;
    ImageView imageView;
    Button button;

    private final static int DIALOG = 1;
    boolean[] flags1 = new boolean[]{false, false};//初始复选情况
    boolean[] flags2 = new boolean[]{false, false, false, false, false, false, false, false, false};
    boolean[] flags3 = new boolean[]{false, false, false, false, false, false, false, false};
    boolean[] flags4 = new boolean[]{false, false};
    String[] items = null;
    String[] items2 = null;
    String[] items3 = null;
    String[] items4 = {"找工作", "找实习"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_wanted);
        String username = getIntent().getStringExtra("username");
        items = getResources().getStringArray(R.array.sex);
        items2 = getResources().getStringArray(R.array.age);
        items3 = getResources().getStringArray(R.array.salary);

        UserInfo userInfo = new UserInfo();
        textView = findViewById(R.id.editText4);//性别
        textView2 = findViewById(R.id.editText5);//年龄
        textView3 = findViewById(R.id.editText9);//薪水
        school = findViewById(R.id.editText6);
//        username=findViewById(R.id.editText3);
        address = findViewById(R.id.editText12);
        wantedjob = findViewById(R.id.editText11);
        workexperience = findViewById(R.id.editText8);
        discribe = findViewById(R.id.editText10);
        major = findViewById(R.id.editText7);
        projectexperience = findViewById(R.id.editText3);
        ty = findViewById(R.id.editTe);

        //返回
        imageView = findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //保存发布
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userInfo.setUsername(username);
                userInfo.setType(ty.getText().toString());
                userInfo.setSalary(textView3.getText().toString());
                userInfo.setWantedjob(wantedjob.getText().toString());
                userInfo.setAddress(address.getText().toString());
                userInfo.setWorkexperience(workexperience.getText().toString());
                userInfo.setMajor(major.getText().toString());
                userInfo.setSchool(school.getText().toString());
                userInfo.setDescribe(discribe.getText().toString());
                userInfo.setAge(textView2.getText().toString());
                userInfo.setSex(textView.getText().toString());
                userInfo.setProjectexperience(projectexperience.getText().toString());
                userInfo.save(new SaveListener<String>() {
                    @Override
                    public void done(String objectId, BmobException e) {
                        if (e == null) {
                            Toast.makeText(addWanted.this, "添加数据成功：", Toast.LENGTH_LONG);
                            finish();
                        } else {
                            Toast.makeText(addWanted.this, "创建数据失败：" + e.getMessage(), Toast.LENGTH_LONG);
                        }
                    }
                });
            }
        });

        LinearLayout sex = (LinearLayout) findViewById(R.id.password);
        sex.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                // 显示对话框
                showDialog(1);
            }
        });

        LinearLayout age = (LinearLayout) findViewById(R.id.password2);
        age.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                // 显示对话框
                showDialog(2);
            }
        });

        LinearLayout salary = (LinearLayout) findViewById(R.id.linearlayout4);
        salary.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                // 显示对话框
                showDialog(3);
            }
        });

        LinearLayout TY = (LinearLayout) findViewById(R.id.ty);
        TY.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                // 显示对话框
                showDialog(4);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {

        Dialog dialog = null;
        switch (id) {
            case 1:
                Builder builder = new Builder(this);
                builder.setIcon(R.drawable.sex);
                builder.setTitle("性别");
                builder.setMultiChoiceItems(items, flags1, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags1[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags1.length; i++) {
                            if (flags1[i]) {
                                results = results + items[i];
                            }
                        }
                        textView.setTextSize(20);
                        textView.setText(results);
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder.create();
                break;

            case 2:
                Builder builder2 = new Builder(this);
                builder2.setIcon(R.drawable.pmessage);
                builder2.setTitle("年龄");
                builder2.setMultiChoiceItems(items2, flags2, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags2[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags2.length; i++) {
                            if (flags2[i]) {
                                results = results + items2[i];
                            }
                        }
                        textView2.setTextSize(20);
                        textView2.setText(results);
                    }
                });
                builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder2.create();
                break;

            case 3:
                Builder builder3 = new Builder(this);
                builder3.setIcon(R.drawable.salarys);
                builder3.setTitle("薪水");
                builder3.setMultiChoiceItems(items3, flags3, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags3[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags3.length; i++) {
                            if (flags3[i]) {
                                results = results + items3[i];
                            }
                        }
                        textView3.setTextSize(20);
                        textView3.setText(results);
                    }
                });
                builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder3.create();
                break;
            case 4:
                Builder builder4 = new Builder(this);
                builder4.setIcon(R.drawable.salarys);
                builder4.setTitle("薪水");
                builder4.setMultiChoiceItems(items4, flags4, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags4[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags4.length; i++) {
                            if (flags4[i]) {
                                results = results + items4[i];
                            }
                        }
                        ty.setTextSize(20);
                        ty.setText(results);
                    }
                });
                builder4.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder4.create();
                break;

            default:
                break;
        }

        return dialog;
    }
}
